package com.nerve.cloudoffice.web.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.nerve.cloudoffice.common.Reply;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @project cloudOffice NerverAction.java
 * @datetime Jul 29, 2013 3:30:15 AM
 * @description 
 * 	所有action的父类   
 * @author 集成显卡 IBM 
 *			zxingming@qq.com
 *			zengxm@cn.ibm.com
 *
 * @change log:
 *
 */
@SuppressWarnings("serial")
public class NerveAction extends ActionSupport{

	Logger logger = Logger.getLogger(this.getClass());
	
	/**
	 * 获取本次的请求实例
	 * @return
	 */
	protected HttpServletRequest getRequest(){
		return ServletActionContext.getRequest();
	}
	
	/**
	 * 向浏览器打印消息
	 * @param msg
	 * @throws Exception
	 */
	protected void print(String msg) throws Exception{
		HttpServletResponse resp = initResponse();
		resp.getWriter().print(msg);
	}
	
	/**
	 * 设置response，默认的编码为utf-8
	 * @return
	 */
	protected HttpServletResponse initResponse(){
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setHeader("content-type", "text/html;charset=utf-8");
		
		return resp;
	}
	
	/**
	 * @method name: json
	 * @description: 输出json数据到浏览器
	 * @return type: void
	 *	@param reply
	 */
	protected void json(Reply reply){
		try{
			boolean isFormat = false;
			
			String format = getRequest().getParameter("format");
			if(format != null && format.equals("1"))
				isFormat = true;
			
			String json = JSON.toJSONString(
					reply, 
					SerializerFeature.DisableCircularReferenceDetect,
					SerializerFeature.WriteMapNullValue,
					isFormat ? SerializerFeature.PrettyFormat:SerializerFeature.SortField
					);
			
			json(json);
		}catch(Exception e){
			error("error on json a Reply:", e);
		}
	}
	
	/**
	 * @method name: json
	 * @description: 以指定的 ValueFilter 输出json数据到浏览器
	 * @return type: void
	 *	@param reply
	 *	@param vf
	 */
	protected void json(Reply reply, ValueFilter vf){
		SerializeWriter writer = new SerializeWriter();
		JSONSerializer out = new JSONSerializer(writer);
		
		out.config(SerializerFeature.DisableCircularReferenceDetect, true);
		out.config(SerializerFeature.WriteMapNullValue, true);
		
		String format = getRequest().getParameter("format");
		if(format != null && format.equals("1"))
			out.config(SerializerFeature.PrettyFormat, true);
		
		out.getValueFilters().add(vf);
		
		out.write(reply);
		try{
			json(out.toString());
		}catch(Exception e){
			error("error on json a Reply:", e);
		}
	}
	
	/**
	 * @method name: json
	 * @description: 输出json
	 * @return type: void
	 *	@param json
	 */
	public void json(String json)throws Exception{
		HttpServletResponse resp = ServletActionContext.getResponse();
		resp.setHeader("content-type", "text/plain;charset=utf-8");
		
		PrintWriter pw = resp.getWriter();
		pw.print(json);
		pw.flush();
		pw.close();
	}
	
	
	protected void log(Object msg){
		logger.info(msg);
	}
	
	protected void error(Object msg){
		logger.error(msg);
	}
	
	protected void error(Object msg, Throwable t){
		logger.error(msg, t);
	}
}